<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Orders_Model extends CI_Model{
	protected $_name = "orders";
	protected $_primary = "OrderId";
	var $Codes = "Codes", $CreateDate = "CreateDate", $StartDate = "StartDate", $EndDate = "EndDate", $TotalPrice = "TotalPrice", $Note = "Note", $SpdId = "SpdId", $PmId = "PmId", $CusId = "CusId", $PayId = "PayId", $UserId = "UserId", $Status = "Status";
	
	function countOrders($status){
		$this->db->where($this->Status, $status);
		$query = $this->db->get($this->_name);
		return $query->num_rows();
	}
	
	function countTotalOrders(){
		$this->db->where($this->Status, 0);
		$this->db->or_where($this->Status, 1);
		$this->db->or_where($this->Status, 2);
		$query = $this->db->get($this->_name);
		return $query->num_rows();
	}
	
	function getOrdersLimit($status, $currentPage, $number){
		$this->db->select($this->_name.'.*, sp.NameService_vn, spd.Duration, c.FullName, pm.NameMethod_vn');
		$this->db->join("service_pack_detail as spd","spd.SpdId=".$this->_name.".".$this->SpdId);
		$this->db->join("service_pack as sp","sp.SpId=spd.SpId");
		$this->db->join("customers as c","c.CusId=".$this->_name.".".$this->CusId);
		$this->db->join("pay_method as pm","pm.PmId=".$this->_name.".".$this->PmId);
		$this->db->where($this->_name.".".$this->Status, $status);
		$this->db->order_by($this->CreateDate, "ASC");
		$this->db->limit($number, $currentPage);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = $currentPage;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function searchOrders($keysearch, $fieldsearch, $status){
		$this->db->select($this->_name.'.*, sp.NameService_vn, spd.Duration, c.FullName, pm.NameMethod_vn');
		$this->db->join("service_pack_detail as spd","spd.SpdId=".$this->_name.".".$this->SpdId);
		$this->db->join("service_pack as sp","sp.SpId=spd.SpId");
		$this->db->join("customers as c","c.CusId=".$this->_name.".".$this->CusId);
		$this->db->join("pay_method as pm","pm.PmId=".$this->_name.".".$this->PmId);
		switch($fieldsearch){
			case 1:
				$this->db->like($this->Codes, $keysearch);	
				break;
			case 2:
				$this->db->like('FullName', $keysearch);	
				break;
			default:
				$this->db->like($this->Codes, $keysearch);	
				break;
		}
		$this->db->where($this->_name.'.'.$this->Status, $status);
		$this->db->order_by($this->CreateDate, "ASC");
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function getOrderByOrderId($orderid, $status){
		$this->db->select($this->_name.'.*, sp.NameService_vn, spd.Duration, c.Email, c.FullName, c.Gender, c.Birthday, c.Address, c.Phone, pm.NameMethod_vn, p.*, u.FullName as NameEmployee');
		$this->db->join("service_pack_detail as spd","spd.SpdId=".$this->_name.".".$this->SpdId);
		$this->db->join("service_pack as sp","sp.SpId=spd.SpId");
		$this->db->join("customers as c","c.CusId=".$this->_name.".".$this->CusId);
		$this->db->join("pay_method as pm","pm.PmId=".$this->_name.".".$this->PmId);
		$this->db->join("payments as p","p.PayId=".$this->_name.".".$this->PayId, "left");
		$this->db->join("users as u","u.UserId=".$this->_name.".".$this->UserId, "left");
		$this->db->where($this->_name.".".$this->_primary, $orderid);
		$this->db->where($this->_name.".".$this->Status, $status);
		$query = $this->db->get($this->_name);
		return $query->row();
	}
	
	function getOrderByOrderDetail($orderid){
		$this->db->select($this->_name.'.*, sp.NameService_vn, spd.Duration,spd.Price, c.*, pm.NameMethod_vn, pm.Images, p.*, u.FullName as NameEmployee');
		$this->db->join("service_pack_detail as spd","spd.SpdId=".$this->_name.".".$this->SpdId);
		$this->db->join("service_pack as sp","sp.SpId=spd.SpId");
		$this->db->join("customers as c","c.CusId=".$this->_name.".".$this->CusId);
		$this->db->join("pay_method as pm","pm.PmId=".$this->_name.".".$this->PmId);
		$this->db->join("payments as p","p.PayId=".$this->_name.".".$this->PayId, "left");
		$this->db->join("users as u","u.UserId=".$this->_name.".".$this->UserId, "left");
		$this->db->where($this->_name.".".$this->_primary, $orderid);
		$query = $this->db->get($this->_name);
		return $query->row();
	}
	function getOrderPaypal($orderid){
		$this->db->select($this->_name.'.*, sp.NameService_en,spd.Duration,spd.Price');
		$this->db->join("service_pack_detail as spd","spd.SpdId=".$this->_name.".".$this->SpdId);
		$this->db->join("service_pack as sp","sp.SpId=spd.SpId");
		$this->db->where($this->_name.".".$this->_primary, $orderid);
		$query = $this->db->get($this->_name);
		return $query->row();
	}
	function insertOrder($cusid){
		$this->db->set($this->Codes, "");
		$this->db->set($this->CreateDate, date('Y-m-d H:i:s'));
		$this->db->set($this->TotalPrice, trim($this->input->post('total_price', true)));
		$this->db->set($this->SpdId, trim($this->input->get('detail', true)));
		$this->db->set($this->CusId, $cusid);
		$this->db->set($this->PmId, trim($this->input->post('paymentmethod',true)));
		$this->db->set($this->Status, 0);
		$this->db->insert($this->_name);
		$id = $this->db->insert_id();
		return $id;
	}
	function updateOrderPay($OrderId,$PayId){
		$data = array(
					$this->PayId => $PayId,
				);
		$this->db->where($this->_primary, $OrderId);
		$this->db->update($this->_name, $data);
	}
	function updateAcceptOrder($OrderId){
		$order = $this->getOrderByOrderId($OrderId, 0);
		$StartDate = date('Y-m-d H:i:s');
		$EndDate = date('Y-m-d H:i:s', strtotime('+'.$order->Duration.' month', strtotime($StartDate)));
		$data = array(
					$this->StartDate => $StartDate,
					$this->EndDate => $EndDate,
					$this->UserId => $this->session->userdata('UserId'),
					$this->Status => 1,
				);
		$this->db->where($this->_primary, $OrderId);
		$this->db->update($this->_name, $data);
	}
	
	function deleteOrder($OrderId){
		try{
			$this->db->where($this->_primary, $OrderId);
			$this->db->update($this->_name, array($this->Status => 3));
			return true;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function updateOrderPayMethod($OrderId,$PmId){
		$data = array(
					$this->PmId => $PmId,
				);
		$this->db->where($this->_primary, $OrderId);
		$this->db->update($this->_name, $data);
	}

}